package ESP.Control.A;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.jcs.JCS;
import org.apache.log4j.Logger;

import ESP.Dao.BaseDao;
import ESP.Util.IDFactory;
import ESP.VO.Role;

import com.alibaba.fastjson.JSONArray;

public class a04 {
	@Resource
	public BaseDao baseDao;
	/**
	 * 日志
	 */
	private Logger logger = Logger.getLogger(a04.class);
	/**
	 * 缓存
	 */
	public static JCS cache_dict ;
	/**
	 * 返回信息
	 */
	private JSONArray rows;
	private Map<String, String> message = new HashMap<String, String>();
	/**
	 * 用户信息
	 */
	private String user_role_id;
	private String user_id;
	/**
	 * 学院字典
	 */
	private String faculty_id;
	private String faculty_name;
	/**
	 * 专业字典
	 */
	private String major_id;
	private String major_name;
	/**
	 * 数据类型字典
	 */
	private String datatype_id;
	private String datatype_name;
	/**
	 * 指定权限
	 */
	private String executeFunction(String returnMsg, String sql, String executeFlag) throws RuntimeException{//executeFlag：list || int
		if (this.user_role_id == null || this.user_role_id.equals("")) {
			//this.rows = null;
			baseDao.setReturnInfo(message, "N", "本次登录会话失效,请重新登录!");
			return returnMsg;
		}
		if (!this.user_role_id.equals(Role.ADMIN) && !this.user_role_id.equals(Role.TEACHER)) {
			//this.rows = null;
			baseDao.setReturnInfo(message, "N", "您无此权限!");
			return returnMsg;
		}
		if (executeFlag.equals("list")) {//结果集为list
			logger.info("【sql list】:"+sql);
			List list = baseDao.querySqlForList(sql);
			baseDao.setReturnInfo(message, "Y", "查询完毕");
			this.rows = baseDao.toJson(list);
			return returnMsg;
		}else if (executeFlag.equals("int")) {//执行更新，结果集为int
			logger.info("【sql int】:"+sql);
			int temp = baseDao.updateSqlForInt(sql);
			baseDao.setReturnInfo(message, "Y", "成功操作"+temp+"条数据!");
			return returnMsg;
		}else {
			return returnMsg;
		}
		
	}
	/**
	 * 查询学院字典
	 * @return
	 */
	@SuppressWarnings("finally")
	public String queryFaculty(){
//		a04.cache_dict = getCache_dict();
//		if (cache_dict == null || cache_dict.get("faculty") == null) {
//			List list = baseDao.querySqlForList("select faculty_id,faculty_name from esp_faculty");
//			this.rows = baseDao.toJson(list);
//			System.out.println("通过sql查询学院字典成功！");
//			try {
//				cache_dict.put("faculty", this.rows);
//				logger.info("缓存学院字典成功！");
//			} catch (CacheException e) {
//				logger.info("缓存学院字典失败！:"+e.getMessage());
//			}finally{
//				return "a04_rows";
//			}
//		}else {
//			System.out.println("通过缓存查询学院字典成功！");
//			this.rows = (JSONArray) cache_dict.get("faculty");
//			return "a04_rows";
//		}
		return this.executeFunction("queryFaculty", "select faculty_id,faculty_name from esp_faculty", "list");
	}
	/**
	 * 删除学院字典
	 * @return
	 */
	public String removeFaculty(){
		//Student、task表外键引用 //解决：1、有外键引用 先不删除；2、提示；3、强制删除功能
		return this.executeFunction("removeFaculty", 
					"delete from esp_faculty where faculty_id='"+this.faculty_id+"'", "int");
	}
	/**
	 * 修改学院字典
	 * @return
	 */
	public String updateFaculty(){
		//Student、task表外键引用 //解决：1、有外键引用 先不删除；2、提示；3、强制删除功能
		return this.executeFunction("updateFaculty", 
					"update esp_faculty set faculty_name='"+this.faculty_name+"' where faculty_id='"+this.faculty_id+"'", "int");
	}
	/**
	 * 增加学院字典
	 * @return
	 */
	public String addFaculty(){
		//Student、task表外键引用 //解决：1、有外键引用 先不删除；2、提示；3、强制删除功能
		if (this.faculty_id == null || this.faculty_id.equals("") || this.faculty_id.length() > 13) {
			this.faculty_id = IDFactory.getID();
		}
		return this.executeFunction("addFaculty", 
				"insert into esp_faculty (faculty_id,faculty_name) values ('"+this.faculty_id+"','"+this.faculty_name+"')", "int");
	}
	
	/**
	 * 查询专业字典
	 * @return
	 */
	@SuppressWarnings("finally")
	public String queryMajor(){
//		a04.cache_dict = getCache_dict();
//		if (cache_dict == null || cache_dict.get("major") == null) {
//			List list = baseDao.querySqlForList("select major_id,major_name from esp_major");
//			this.rows = baseDao.toJson(list);
//			System.out.println("通过sql查询专业字典成功！");
//			try {
//				cache_dict.put("major", this.rows);
//				logger.info("缓存专业字典成功！");
//			} catch (CacheException e) {
//				logger.info("缓存专业字典失败！:"+e.getMessage());
//			}finally{
//				return "a04_rows";
//			}
//		}else {
//			System.out.println("通过缓存查询专业字典成功！");
//			this.rows = (JSONArray) cache_dict.get("major");
//			return "a04_rows";
//		}
		return this.executeFunction("queryMajor", "select major_id,major_name from esp_major", "list");
	}
	/**
	 * 删除专业字典
	 * @return
	 */
	public String removeMajor(){
		return this.executeFunction("removeMajor", 
					"delete from esp_major where major_id='"+this.major_id+"'", "int");
	}
	/**
	 * 修改专业字典
	 * @return
	 */
	public String updateMajor(){
		//Student、task表外键引用 //解决：1、有外键引用 先不删除；2、提示；3、强制删除功能
		return this.executeFunction("updateMajor", 
					"update esp_major set major_name='"+this.major_name+"' where major_id='"+this.major_id+"'", "int");
	}
	/**
	 * 增加专业字典
	 * @return
	 */
	public String addMajor(){
		//Student、task表外键引用 //解决：1、有外键引用 先不删除；2、提示；3、强制删除功能
		if (this.major_id == null || this.major_id.equals("") || this.major_id.length() > 13) {
			this.major_id = IDFactory.getID();
		}
		return this.executeFunction("addMajor", 
				"insert into esp_major (major_id,major_name) values ('"+this.major_id+"','"+this.major_name+"')", "int");
	}
	
	/**
	 * 查询数据类型字典
	 * @return
	 */
	@SuppressWarnings("finally")
	public String queryDatatype(){
//		a04.cache_dict = getCache_dict();
//		if (cache_dict == null || cache_dict.get("datatype") == null) {
//			List list = baseDao.querySqlForList("select type_id datatype_id,type_name datatype_name from esp_datatype order by type_name");
//			this.rows = baseDao.toJson(list);
//			System.out.println("通过sql查询数据类型字典成功！");
//			try {
//				cache_dict.put("datatype", this.rows.toString());
//				logger.info("缓存数据类型字典成功！");
//			} catch (CacheException e) {
//				logger.info("缓存数据类型字典失败！:"+e.getMessage());
//			}finally{
//				return "a04_rows";
//			}
//		}else {
//			System.out.println("通过缓存查询数据类型字典成功！");
//			this.rows = (JSONArray) cache_dict.get("datatype");
//			return "a04_rows";
//		}
		return this.executeFunction("queryDatatype", "select type_id datatype_id,type_name datatype_name from esp_datatype order by type_name", "list");
	}
	/**
	 * 删除数据类型字典
	 * @return
	 */
	public String removeDatatype(){
		return this.executeFunction("removeDatatype", 
					"delete from esp_datatype where type_id='"+this.datatype_id+"'", "int");
	}
	/**
	 * 修改数据类型字典
	 * @return
	 */
	public String updateDatatype(){
		//Student、task表外键引用 //解决：1、有外键引用 先不删除；2、提示；3、强制删除功能
		return this.executeFunction("updateDatatype", 
					"update esp_datatype set type_name='"+this.datatype_name+"' where type_id='"+this.datatype_id+"'", "int");
	}
	/**
	 * 增加数据类型字典
	 * @return
	 */
	public String addDatatype(){
		//Student、task表外键引用 //解决：1、有外键引用 先不删除；2、提示；3、强制删除功能
		if (this.datatype_id == null || this.datatype_id.equals("") || this.datatype_id.length() > 13) {
			this.datatype_id = IDFactory.getID();
		}
		return this.executeFunction("addDatatype", 
				"insert into esp_datatype (type_id,type_name) values ('"+this.datatype_id+"','"+this.datatype_name+"')", "int");
	}
	public String testTypeId(){
		if (this.datatype_id == null || this.datatype_id.trim().equals("")) {
			message.put("returnMsg", "请输入一个正确的数据格式作为主码!");
			message.put("returnFlag", "N");
			return "testTypeId";
		}
		int temp = 
			baseDao.querySqlForInt("select count(*) from esp_datatype where type_id='"+this.datatype_id+"'");
		if (temp == 0) {
			message.put("returnMsg", "通过验证");
			message.put("returnFlag", "Y");
		}else {
			message.put("returnMsg", "此格式已经存在");
			message.put("returnFlag", "N");
		}
		return "testTypeId";
	}
	
	//get set
	public JCS getCache_dict() {
		if (cache_dict != null) {
			return a04.cache_dict;
		}
		try {
			cache_dict = JCS.getInstance("cacheDict");
			return cache_dict;
		} catch (Exception e) {
			logger.info("获取字典缓存异常:"+e.getCause());
			return null;
		}
	}
	public BaseDao getBaseDao() {
		return baseDao;
	}
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	public JSONArray getRows() {
		return rows;
	}
	public void setRows(JSONArray rows) {
		this.rows = rows;
	}
	public String getUser_role_id() {
		return user_role_id;
	}
	public void setUser_role_id(String userRoleId) {
		user_role_id = userRoleId;
	}
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String userId) {
		user_id = userId;
	}
	public String getFaculty_id() {
		return faculty_id;
	}
	public void setFaculty_id(String facultyId) {
		faculty_id = facultyId;
	}
	public String getFaculty_name() {
		return faculty_name;
	}
	public void setFaculty_name(String facultyName) {
		faculty_name = facultyName;
	}
	public String getMajor_id() {
		return major_id;
	}
	public void setMajor_id(String majorId) {
		major_id = majorId;
	}
	public String getMajor_name() {
		return major_name;
	}
	public void setMajor_name(String majorName) {
		major_name = majorName;
	}
	public String getDatatype_id() {
		return datatype_id;
	}
	public void setDatatype_id(String datatypeId) {
		datatype_id = datatypeId;
	}
	public String getDatatype_name() {
		return datatype_name;
	}
	public void setDatatype_name(String datatypeName) {
		datatype_name = datatypeName;
	}
	public Map<String, String> getMessage() {
		return message;
	}
	public void setMessage(Map<String, String> message) {
		this.message = message;
	}
}
